package com.health.notice.controller;

import java.util.List;

import com.health.common.constant.CacheConstants;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.BusinessType;
import com.health.notice.domain.ContentNotice;
import com.health.notice.service.IContentNoticeService;
import com.health.common.utils.poi.ExcelUtil;
import com.health.common.core.page.TableDataInfo;

/**
 * 公告管理Controller
 * 
 * @author moxxie
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/notice/notice")
public class ContentNoticeController extends BaseController
{
    @Autowired
    private IContentNoticeService contentNoticeService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询公告管理列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContentNotice contentNotice)
    {
        startPage();
        List<ContentNotice> list = contentNoticeService.selectContentNoticeList(contentNotice);
        return getDataTable(list);
    }

    /**
     * 导出公告管理列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:export')")
    @Log(title = "公告管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContentNotice contentNotice)
    {
        List<ContentNotice> list = contentNoticeService.selectContentNoticeList(contentNotice);
        ExcelUtil<ContentNotice> util = new ExcelUtil<ContentNotice>(ContentNotice.class);
        util.exportExcel(response, list, "公告管理数据");
    }

    /**
     * 获取公告管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        return success(contentNoticeService.selectContentNoticeByNoticeId(noticeId));
    }

    /**
     * 新增公告管理
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:add')")
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContentNotice contentNotice)
    {
        redisTemplate.opsForSet().add(CacheConstants.TMP_VALID_FILE_SET, contentNotice.getContent());
        return toAjax(contentNoticeService.insertContentNotice(contentNotice));
    }

    /**
     * 修改公告管理
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:edit')")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContentNotice contentNotice)
    {
        redisTemplate.opsForSet().add(CacheConstants.TMP_VALID_FILE_SET, contentNotice.getContent());
        return toAjax(contentNoticeService.updateContentNotice(contentNotice));
    }

    /**
     * 删除公告管理
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:remove')")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(contentNoticeService.deleteContentNoticeByNoticeIds(noticeIds));
    }

    /**
     * 修改公告状态
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:edit')")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ContentNotice contentNotice)
    {
        return toAjax(contentNoticeService.updateContentNoticeStatus(contentNotice));
    }
}
